package com.appkudos.view;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.appkudos.controller.GroupController;
import com.appkudos.controller.UserController;
import com.appkudos.model.Category;
import com.appkudos.model.Group;
import com.appkudos.model.User;
import com.appkudos.view.R;

public class CreateGroupActivity extends Activity{
    /** Called when the activity is first created. */

	EditText groupname;
	Spinner category;
	EditText desc;
	Button createButton;
	
	
	GroupController gc = new GroupController();
	List<Category> categories;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_create_group);
        initWidget();
    }
    
    private void initWidget(){
    	groupname = (EditText) findViewById(R.id.cg_group_name);
    	category = (Spinner) findViewById(R.id.cg_category);
    	fillCategory();
    	desc = (EditText) findViewById(R.id.cg_desc_name);
    	createButton = (Button) findViewById(R.id.cg_b_create);
    	createButton.setOnClickListener(myListener);
    }
    
    private void fillCategory(){
    	categories = gc.getAllCategories();
    	
    	ArrayAdapter <CharSequence> adapter =
    		  new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item);
    	adapter.add("Choose any category");
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	for (int i=0; i<categories.size(); i++){
    		adapter.add(categories.get(i).getCatName());
    	}
    	
    	category.setAdapter(adapter); 
    	
    }
    
    private OnClickListener myListener = new OnClickListener() {
    	public void onClick(View v){
    		
    		String GroupName = groupname.getText().toString();
    		int Cat = category.getSelectedItemPosition();
    		//check if the info is complete
    		if (GroupName.equals("") || Cat == 0){
    			Toast.makeText(getApplicationContext(), "Information is missing",Toast.LENGTH_LONG).show();
    		}else{
    			gc.createGroup(GroupName, desc.getText().toString(), categories.get(Cat-1).getCatid());
    			Toast.makeText(getApplicationContext(), "Group created",Toast.LENGTH_LONG).show();
    			
    		}
    		
    		
    	}
    };
}